<?php
namespace app\admin\controller;
use app\admin\model\ossModel;
use think\Controller;
use think\Db;
class Adminoss extends ossModel{

    public function index()
    {
        error_reporting(0);
        header("Content-type:text/html;charset=utf-8");
        if($this->request->isPost()){
            $object = self::get_rand_str(5);//上传图片成功后的图片名
            $data = self::imgdata($object);//上传路径

            if(!empty($_POST['id'])){//店铺环境图片
                $content = $_POST['dataURL'];
                $thumb['id'] = $_POST['id'];
                $thumb['type'] = 2;
            }else{//普通图片
                $content = file_get_contents('php://input');//获取base64
            }
            preg_match('/^(data:\s*image\/(\w+);base64,)/',$content,$result);//获取base64图片

            $type = $result[2];//图片后缀
            $new_file = $_SERVER['DOCUMENT_ROOT'].'/static/upload/';//上传本地路径
            $filename = time() . '_' . uniqid() . ".{$type}"; //文件名
            $new_file = $new_file . $filename;

            if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $content)))) {
                $content = file_get_contents($new_file);
                $info = $this->moveOss('LTAIc2rnHp53SkQF','dVyjVLHSpgO8jenQYIXKVMBXoEOq1D','http://oss-cn-shanghai.aliyuncs.com','zdtx023',$data['path'].$data['name'].$data['not'],$content);

                if(!empty($info))unlink($new_file);//如果上传成功将上传本地图片删除;
                $arr2[] = $info;
                $result = implode(';',$arr2);
            }

            if(!empty($result))$id = $this->editImg($data,$thumb);
            if(!empty($id)){
                echo json_encode(['result'=>$result,'id'=>$id]);
            }
        }else{
            return view();
        }
    }


    public function editImg($url = '',$thumb=0){

        if(isset($thumb['type'])&&$thumb['type'] == 2) {//店铺图片组编辑
            $data['fileName'] = $url['name'] . $url['not'];
            $data['picurl'] = $url['path'] . $url['name'] . $url['not'];
            Db::name('picture')->insert($data);
            $id = Db::name('picture')->order('id DESC')->value('id');
            $picture_id = Db::name('shop')->where(['id' => $thumb['id']])->value('picture_id');
            $picture_id == '' ? $picture = $id : $picture = $picture_id . ',' . $id;//如果是第一张就没有,
            $edits['picture_id'] = $picture;
            $result = Db::name('shop')->where(['id' => $thumb['id']])->update($edits);
        }else{//普通图片上传
            $data['fileName'] = $url['name'].$url['not'];
            $data['picurl'] = $url['path'].$url['name'].$url['not'];
            Db::name('picture')->insert($data);
            $id = Db::name('picture')->order('id DESC')->value('id');
        }
        if(!$id){
            return false;
        }else{
            return $id;
        }
    }
    public function imgdata($name=''){
        $data = [];
        $not ='.png';
        $path = 'zudao/';
        //图片名字
        $data['name'] = md5($name);
        $data['not'] = $not;
        $data['path'] = $path;
        return $data;
    }

    //随机字符串
    public function get_rand_str($len)
    {
        $str = "1234567890asdfghjklqwertyuiopzxcvbnmASDFGHJKLZXCVBNMPOIUYTREWQ";
        return substr(str_shuffle($str), 0, $len);
    }
}